<?php
/**
 * Created by PhpStorm.
 * User: Argon
 * Date: 2016/12/14
 * Time: 15:14
 */

namespace Admin\Controller;

use Admin\Model\AdminModel;
use Think\Controller;
use Think\Verify;

class AdminController extends Controller
{
    private $_model;

    public function _initialize()
    {
        $this->_model = D("Admin");
    }

    public function listAll($keyword='')
    {
        $cond=[];
        $keyword=trim($keyword);
        if ($keyword) {
            $cond['name'] = [
                'like', '%' . $keyword . '%'
            ];
        }
        $data=$this->_model->getPageData($cond);
        $data['keyword']=$keyword;

        $this->assign($data);
        $this->display();
    }

    public function add()
    {
        if (IS_POST) {
            if ($this->_model->create('', 'add')===false) {
                $this->error(listError($this->_model),U("Admin/add"));
                exit;
            }
            if ($this->_model->addAdmin() === false) {
                $this->error(listError($this->_model),U("Admin/add"));
                exit;
            }
            $this->success('添加成功',U("Admin/listAll"));
            exit;
        }
        $data['title'] = '管理员添加';
        $this->assign($data);
        $this->display('add');
        exit;
    }


    public function edit($id){
        if(IS_POST){
            if ($this->_model->create('', 'edit')===false) {
                $this->error(listError($this->_model),U("Admin/edit",['id'=>I("post.id")]));
            }
            if($this->_model->save()===false){
                $this->error(listError($this->_model),U("Admin/edit",['id'=>I("post.id")]));
            }
            $this->success('修改成功',U("Admin/listAll"));

        }


        $data=$this->_model->find($id);
        $data['title']='编辑管理员信息';

        $this->assign($data);
        $this->display('edit');

    }

    public function login(){
        if(IS_POST){
            $code=I("param.code");
//            if ($this->check_verify($code,1)===false) {
//                $this->error("验证码错误",U("Admin/login"));
//                exit;
//            }
            $data=$this->_model->where(['name'=>I("param.name")])->find();
            if(empty($data)){
                $this->error("无此用户",U("Admin/login"));
                exit;
            }
            if($data['password']!=md5(md5(I("param.password")).$data['salt'])){
                $this->error("密码错误",U("Admin/login"));
                exit;
            }
            //TODO:权限分配
            $admin_info=[
              'is_login'=>'1','name'=>$data['name'],'id'=>$data['id'],
            ];
            session('admin_info',$admin_info);
            $this->success('登录成功',U("Index/index"));
            exit;
        }
        $this->display();
    }

    private function  check_verify($code, $id = ''){
        $verify = new Verify();
        return $verify->check($code, $id);
    }

    public function logout(){
        session(null);
        $this->success('退出成功',U("Index/index"));
    }

    public function delOne($id){
        if ($this->_model->delete($id)===false) {
            $this->error('删除错误',U('Admin/listAll'));
        }
        $this->success('删除成功',U("Admin/listAll"));
    }

    public function changePwd($id){
        if(IS_POST){
            $salt=array_pop($this->_model->field('salt')->find($id));
            if($this->_model->create('','chpwd')===false){
                $this->error(listError($this->_model),U("Admin/edit",['id'=>I("post.id")]));
            }
            $password=md5($this->_model->data()['password'].$salt);
            if($this->_model->save(['id'=>$id,'password'=>$password])===false){
                $this->error(listError($this->_model),U("Admin/edit",['id'=>I("post.id")]));
            }
            $this->success('修改完成',U("Admin/listAll"));
            exit;
        }
        $data=$this->_model->find($id);
        $this->assign($data);
        $this->display();
    }

    public function selfChpwd(){
        $id=session('admin_info')['id'];
        if(IS_POST){
            $salt=array_pop($this->_model->field('salt')->find($id));
            if($this->_model->create('','chpwd')===false){
                $this->error(listError($this->_model),U("Admin/listAll"));
            }
            $password=md5($this->_model->data()['password'].$salt);
            if($this->_model->save(['id'=>$id,'password'=>$password])===false){
                $this->error(listError($this->_model),U("Admin/listAll"));
            }
            $this->success('修改完成',U("Admin/listAll"));
            exit;
        }
        $this->assign('id',$id);
        $this->display();

    }

}